//
//  MyViewController.swift
//  BirthdayManager
//
//  Created by Aaron_Chan on 2020/12/21.
//  Copyright © 2020 Charles. All rights reserved.
//

import UIKit

class MyViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    @IBOutlet weak var CustomImage: UIImageView!
    @IBOutlet weak var nameText: UITextField!
    @IBOutlet weak var dateText: UITextField!
    
    @IBAction func changeInfo(_ sender: Any) {
        saveInfo()
    }
    var information: MyInfo?
    
    @IBAction func uploadImage(_ sender: Any) {
        let imagePicker = UIImagePickerController()
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary) {
            imagePicker.delegate = self
            imagePicker.sourceType = .photoLibrary
            present(imagePicker, animated: true, completion: nil)
        }
    }
    
    @IBAction func buttonUpload(_ sender: Any) {
        let imagePicker = UIImagePickerController()
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary) {
            imagePicker.delegate = self
            imagePicker.sourceType = .photoLibrary
            present(imagePicker, animated: true, completion: nil)
        }
    }
    
    func saveInfo(){
        information?.date = dateText.text
        information?.name = nameText.text
        information?.avatar = CustomImage.image
        let success = NSKeyedArchiver.archiveRootObject(information , toFile: MyInfo.userPath)
        if !success{
            print("failed...")
        }
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
        self.information?.avatar = selectedImage
        self.CustomImage.image = selectedImage
        saveInfo()
        dismiss(animated: true, completion: nil)
    }
    
    func loadInfo(){
        if let tempInfo = NSKeyedUnarchiver.unarchiveObject(withFile:MyInfo.userPath) as?
            MyInfo{
            information = tempInfo
            print("load file successfully")
            nameText.text = information?.name
            dateText.text = information?.date
            CustomImage.image = information?.avatar
        }
    }
    
    func initialInfo() -> Void{
        nameText.text = ""
        dateText.text = "2000/01/01"
        CustomImage.image = nil
        information = MyInfo.init(name: "",date: "2000/01/01",avatar: nil)
        saveInfo()
        loadInfo()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        CustomImage.layer.cornerRadius=75
        CustomImage.layer.masksToBounds=true
        CustomImage.contentMode=UIView.ContentMode.scaleAspectFill
        CustomImage.clipsToBounds = true
        // Do any additional setup after loading the view.
        loadInfo()
        if information == nil{
            initialInfo()
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
